package com.tmptSource.service.impl;

import com.tmpt.dto.Result;
import com.tmpt.utils.DateTool;
import com.tmpt.service.impl.DataLimitService;
import com.tmptSource.mapper.TInfoGoodsMapper;
import com.tmptSource.bean.TInfoGoods;
import com.tmptSource.dto.TInfoGoodsDto;
import com.tmptSource.dto.PageDto;
import com.tmptSource.service.intf.ITInfoGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tmptSource.utils.dozer.MyMapper;
import com.tmptSource.utils.page.MyPageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.dozer.Mapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.apache.log4j.Logger;

/**
 * Created by wxy on 2016/7/28.
 */
@Service(value="tInfoGoodsService")
@Transactional(propagation = Propagation.REQUIRED)
public class TInfoGoodsService implements ITInfoGoodsService {

    private Logger log = Logger.getLogger(this.getClass());
    @Autowired private DataLimitService dataLimitService;
    @Autowired private Mapper mapper;
    @Autowired private MyMapper myMapper;
    @Autowired private MyPageUtils myPageUtils;
    @Autowired
    private TInfoGoodsMapper tInfoGoodsMapper;


    @Override
    public Result<TInfoGoodsDto> pagination(TInfoGoodsDto tInfoGoodsDto,PageDto pageDto) {
        //分页查询
        Result<TInfoGoodsDto> result = myPageUtils.pagination(
                tInfoGoodsDto,pageDto,
                TInfoGoods.class,
                TInfoGoodsDto.class,
                tInfoGoodsMapper,
                false,null);

        return result;
    }

    /**
    * 分布查询，有数据边界控制功能
    * @param tInfoGoodsDto
    * @param pageDto
    * @param paramMap
    * @return
    */
    @Override
    public Result<TInfoGoodsDto> pagination(TInfoGoodsDto tInfoGoodsDto,PageDto pageDto,Map<String,Object> paramMap) {
        //数据边界控制参数
        Map<String,Object> paramMap1 = dataLimitService.getLimitParam(paramMap);

        //分页查询
        Result<TInfoGoodsDto> result = myPageUtils.pagination(
                tInfoGoodsDto,pageDto,
                TInfoGoods.class,
                TInfoGoodsDto.class,
                tInfoGoodsMapper,
                paramMap1,
                false,null);

        return result;
    }


    @Override
    public Result<TInfoGoodsDto> selectAll() {
        Result<TInfoGoodsDto> result = new Result();

        //排序
        PageDto pageDto = new PageDto();
        pageDto.setSortColumn("id");
        pageDto.setSortOrder("desc");
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("page",pageDto);

        List<TInfoGoods> list = tInfoGoodsMapper.selectAll(mapParam);
        List<TInfoGoodsDto> dtoList = myMapper.map(list, TInfoGoodsDto.class);
        result.setResultList(dtoList);

        return result.success();
    }

    @Override
    public Result<TInfoGoodsDto> selectById(long id) {
        Result<TInfoGoodsDto> result = new Result();

        TInfoGoods tInfoGoods = tInfoGoodsMapper.selectById(id);
        TInfoGoodsDto dto = mapper.map(tInfoGoods,TInfoGoodsDto.class);
        result.setResultObj(dto);

        return result.success();
    }


    /**
        返回ID值
    */
    @Override
    public Result<TInfoGoodsDto> addData(TInfoGoodsDto tInfoGoodsDto){
        Result<TInfoGoodsDto> result = new Result();

        //封闭数据
        TInfoGoods tInfoGoods = mapper.map(tInfoGoodsDto, TInfoGoods.class);
        tInfoGoods.setCreatedate(DateTool.getCurTimestamp());

        //调用mapper
        long i = tInfoGoodsMapper.insert(tInfoGoods);
        if(i==0){
            return result.fault("插入失败");
        }

        TInfoGoodsDto dto = mapper.map(tInfoGoods,TInfoGoodsDto.class);
        result.setResultObj(dto);

        return result.success();
    }

    /**
        返回ID值
    */
    @Override
    public Result<TInfoGoodsDto> editData(TInfoGoodsDto tInfoGoodsDto){
        Result<TInfoGoodsDto> result = new Result();

        //封装数据
        TInfoGoods tInfoGoods = mapper.map(tInfoGoodsDto, TInfoGoods.class);
        tInfoGoods.setModifydate(DateTool.getCurTimestamp());

        //调用mapper
        long i = tInfoGoodsMapper.updateById(tInfoGoods);
        if(i==0){
            return result.fault("编辑数据失败");
        }

        TInfoGoodsDto dto = mapper.map(tInfoGoods,TInfoGoodsDto.class);
        result.setResultObj(dto);

        return result.success();
    }

    /**
    伪删除记录，返回ID值
    */
    @Override
    public Result deleteById(long id,String adminMan){
        Result result = new Result();

        Map<String,Object> mapParameter = new HashMap();
        mapParameter.put("id",id);
        mapParameter.put("modifyman",adminMan);
        mapParameter.put("modifydate",DateTool.getCurTimestamp());

        //调用mapper
        long i = tInfoGoodsMapper.deleteById(mapParameter);
        if(i==0){
            return result.fault("删除数据失败");
        }

        return result.success();
    }





}
